<?php
namespace Home\Controller;
header('content-type:text/html;charset=utf-8');
use Think\Controller;
use Org\Type\CatTree;
class IndexController extends Controller {

    public function index(){
    	//实例化商品分类
    	$cats=M('Cats');
        //查询所有类别
        $allcats=$cats->field('catsid,catsname,pid,path')->select();
        //调用分类函数
        $type = CatTree::getlist($allcats);
        //为模板变量赋值
        $this->assign('type',$type);
        //实例化商品
        $good = M("Goods");
        //查询所有的顶级类别
        $topcats=$cats->where('pid=0')->select();
        //dump($topcats);
        //遍历
        foreach($topcats as $k=>$v){
            //查询每个顶级类别下面的最新8个商品
            $gd=$good->where('gcats='.$v['catsid'])->order('timestamp desc')->limit('8')->select();
            //遍历每个商品的id和图片地址
            foreach ($gd as $key => $value) {
                $topcats[$k]['goods'][$value['goodsid']]=$value['goodspic'];
            }
        }
        //为模板变量赋值
        $this->assign('topcats',$topcats);
        //轮播图
        $img=M('img');  //实例化模型
        $imgs=$img->where('state="已发布"')->select();    //查询状态为“已发布”的图片
        $this->assign('lubn',$imgs);   //为模板变量赋值
        $this -> display();
       
    }
}